package com.xm.adv.Ctr;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.baishi.zxlibrary.activity.CodeUtils;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.xm.adv.R;
import com.xm.adv.viewholder.CrossBannerImgHolderView;

import java.util.ArrayList;
import java.util.List;

import xinya.com.baselibrary.bean.program.PositionBean;
import xinya.com.baselibrary.bean.program.ResourcesBean;
import xinya.com.baselibrary.bean.program.SceneBean;
import xinya.com.baselibrary.utils.LogUtil;
import xinya.com.baselibrary.utils.gson.GsonUtils;
import xinya.com.baselibrary.valueFinal.ResourceType;

/**
 * Created by zhangjiebo on 2019/6/22.
 *
 * @author ZhangJieBo
 */
public class DrawCtr {
    public static List<VideoView> draw(Context mContext, RelativeLayout viewDraw, SceneBean sceneBean01,float zoom,int roate) {
//        zoom =1;
        viewDraw.setLayoutParams(new LinearLayout.LayoutParams((int) sceneBean01.getWidth(), (int) (sceneBean01.getHeight())));
        viewDraw.setRotation(roate*90);
        viewDraw.setScaleY(zoom);
        List<VideoView> videoViews = new ArrayList<>();
        try {
//            viewDraw.setVisibility(View.VISIBLE);
            viewDraw.removeAllViews();
            if (!TextUtils.isEmpty(sceneBean01.getBackground())) {
                viewDraw.setBackgroundColor(Color.parseColor(sceneBean01.getBackground()));
            }
//            PositionBeanDao positionBeanDao = daoMaster.newSession().getPositionBeanDao();
//            List<PositionBean> positionBeanList = positionBeanDao.queryBuilder().where(PositionBeanDao.Properties.Keyid.eq(sceneBean01.getId())).list();
            List<PositionBean> positionBeanList = sceneBean01.getPositionBeans();
//            LogUtil.LogShitou("MainActivity--drawAdvpositionBeanList", "" + GsonUtils.parseObject(positionBeanList));
            for (int i = 0; i < positionBeanList.size(); i++) {
                PositionBean positionBean = positionBeanList.get(i);

                List<ResourcesBean> resourcesBeanList = positionBeanList.get(i).getResourcesBeans();
                if (positionBean.getType() == ResourceType.img) {
                    //轮播图
                    ConvenientBanner banner = new ConvenientBanner(mContext);
                    setViewPosition(positionBean, banner);
                    banner.setCanLoop(true);
//                    LogUtil.LogShitou("MainActivity--drawAdvresourcesBeanList", "" + GsonUtils.parseObject(resourcesBeanList));
                    if (resourcesBeanList.size() > 0) {
                        banner.setPages(() -> new CrossBannerImgHolderView(), resourcesBeanList)
                                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator})
                                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
                        if (resourcesBeanList.size() > 1) {
                            if (TextUtils.isEmpty(positionBean.getDuration())) {
                                banner.setScrollDuration(5000);
                                banner.startTurning(5000);
                            } else {
                                int time = Integer.parseInt(positionBean.getDuration());
                                banner.setScrollDuration(time);
                                banner.startTurning(time);
                            }
                            banner.setCanLoop(true);
                        } else {
                            banner.setCanLoop(false);
                            banner.startTurning(0);
                        }
                    } else {
                        banner.setVisibility(View.GONE);
                    }
                    viewDraw.addView(banner);
                } else if (positionBean.getType() == ResourceType.video) {
                    //视频
                    videoViews.clear();
                    if (resourcesBeanList.size() > 0) {
                        ResourcesBean resourcesBean = resourcesBeanList.get(0);
//                            positionBean.setX("0");
//                            positionBean.setY("0");
                        VideoView videoView = new VideoView(mContext);
                        setViewPosition(positionBean, videoView);
                        String filePath = resourcesBean.getFilePath();
                        videoView.setVideoPath(filePath);
                        final int[] videoPostition = {0};
                        videoView.setRotation(0);
                        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                LogUtil.LogShitou("DrawCtr--onPrepared", "准备好了");
                                mp.start();
                                if (viewDraw.getVisibility()==View.INVISIBLE){
                                    videoView.start();
                                    viewDraw.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (videoView.canPause()){
                                                videoView.pause();
                                            }
                                        }
                                    },50);
                                }
                                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                                    @Override
                                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                                            videoView.setBackgroundColor(Color.TRANSPARENT);
                                        return true;
                                    }
                                });
                            }
                        });
                        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                // TODO Auto-generated method stub
                                videoPostition[0]++;
                                if (videoPostition[0] >= resourcesBeanList.size()) {
                                    videoPostition[0] = 0;
                                }
                                try {
                                    videoView.stopPlayback();
                                    videoView.setVideoPath(resourcesBeanList.get(videoPostition[0]).getFilePath());
                                    if (viewDraw.getVisibility()==View.VISIBLE){
                                        mp.start();
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();

                                }

                            }

                        });
//                        myJzvdStd.setUp(resourcesBeanList.get(0).getDownloadurl()
//                                , "饺子快长大");
//                        myJzvdStd.titleTextView.setVisibility(View.GONE);
//                        myJzvdStd.startVideo();
                        viewDraw.addView(videoView);
                        videoViews.add(videoView);
                    }

                } else if (positionBean.getType() == ResourceType.text) {
                    if (resourcesBeanList.size() > 0) {
//                            positionBean.setX("50");
//                            positionBean.setY("650");
                        TextView textView = new TextView(mContext);
                        setViewPosition(positionBean, textView);
                        ResourcesBean resourcesBean = resourcesBeanList.get(0);
                        textView.setTextColor(Color.parseColor(resourcesBean.getTypefacecolour()));
                        if (TextUtils.isEmpty(resourcesBean.getTypefacesize())) {
                            textView.setTextSize(24);
                        } else {
                            textView.setTextSize(Float.parseFloat(resourcesBean.getTypefacesize()));
                        }
//                        textView.initScrollTextView(mContext.getWindowManager(), resourcesBean.getTypefacecontent(), 1);
                        textView.setText(resourcesBean.getTypefacecontent());
//                        textView.starScroll();
                        viewDraw.addView(textView);
                    }
                } else if (positionBean.getType() == ResourceType.qrCode) {
                    //二维码
                    if (resourcesBeanList.size() > 0) {
                        ImageView imageView = new ImageView(mContext);
                        setViewPosition(positionBean, imageView);
                        ResourcesBean resourcesBean = resourcesBeanList.get(0);
                        Bitmap mBitmap2 = CodeUtils.createImage(resourcesBean.getDownloadurl(), 1, 400, 400, null);
                        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        imageView.setImageBitmap(mBitmap2);
                        viewDraw.addView(imageView);
                    }
                } else if (positionBean.getType() == ResourceType.time) {
                    if (resourcesBeanList.size() > 0) {
                        TextView textView = new TextView(mContext);
                        setViewPosition(positionBean, textView);
                        ResourcesBean resourcesBean = resourcesBeanList.get(0);
                        LogUtil.LogShitou("MainActivity--drawAdv日期", "" + GsonUtils.parseObject(resourcesBean));
                        textView.setText(resourcesBean.getZone());
                        if (TextUtils.isEmpty(resourcesBean.getTypefacesize())) {
                            textView.setTextSize(24);
                        } else {
                            textView.setTextSize(Float.parseFloat(resourcesBean.getTypefacesize()));
                        }
                        textView.setTextColor(Color.parseColor(resourcesBean.getTypefacecolour()));
                        viewDraw.addView(textView);
                    }
                }
            }
        } catch (Exception e) {
            LogUtil.LogShitou("SamplePagerAdapter--instantiateItem", "报错了"+ sceneBean01.getId());
        }
        return videoViews;
    }

    private static void setViewPosition(PositionBean positionBean, View view) {
        float width = Float.parseFloat(positionBean.getLongX().replace("px", ""));
        float height = Integer.parseInt(positionBean.getWide().replace("px", ""));
        float x = TextUtils.isEmpty(positionBean.getX())?0:Float.parseFloat(positionBean.getX().replace("px", ""));
        float y = TextUtils.isEmpty(positionBean.getY())?0:Float.parseFloat(positionBean.getY().replace("px", ""));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)width, (int)height);
        params.leftMargin = (int) x;
        params.topMargin = (int) y;
        view.setLayoutParams(params);
    }

}
